package com.zhiche.lisa.bms.controller.bill;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.bill.BillIo;
import com.zhiche.lisa.bms.dao.model.price.PriceIncoming;
import com.zhiche.lisa.bms.pojo.vo.common.CommonVO;
import com.zhiche.lisa.bms.service.bill.BillIoService;
import com.zhiche.lisa.bms.service.interfacelog.InterfaceLogService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 客户或承运商账单 前端控制器
 * </p>
 *
 * @author WangShuiJin
 * @since 2018-09-06
 */
@RestController
@RequestMapping("/billIo")
@Api(value = "/priceIncoming", description = "客户或承运商账单接口", tags = {"客户或承运商账单接口"})
public class BillIoController {
    private static final Logger LOGGER = LoggerFactory.getLogger(BillIoController.class);

    @Autowired
    private BillIoService billIoService;
    @Autowired
    private InterfaceLogService bmsInterfaceLogService;

    @PostMapping(value = "/queryBillIoPage")
    @ApiOperation(value = "查询客户或承运商【账单列表】（分页）", notes = "根据账单类型，得到客户或承运商【账单列表】", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<BillIo>> queryBillIoPage(@RequestBody Page<BillIo> page, @RequestParam(required = true) String billType) {
        RestfulResponse<Page<BillIo>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Page<BillIo> pageBillIoData = billIoService.queryBillIoPage(page, billType);
            result.setData(pageBillIoData);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.queryBillIoPage ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/queryBillIoCustomerList")
    @ApiOperation(value = "查询客户或承运商的【客户列表】", notes = "根据账单类型，得到客户或承运商【客户列表】", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<BillIo>> queryBillIoCustomerList(@RequestParam(required = true) String billType) {
        RestfulResponse<List<BillIo>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<BillIo> pageBillIoData = billIoService.queryBillIoCustomerList(billType);
            result.setData(pageBillIoData);
        } catch (BaseException e) {
            LOGGER.error("/BillIoController.queryBillIoCustomerList ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.queryBillIoCustomerList ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/getBillStatus")
    @ApiOperation(value = "查询客户或承运商的【账单状态列表】", notes = "根据账单类型，得到客户或承运商【账单状态列表】", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> getBillStatus(@RequestParam(required = true) String billType) {
        RestfulResponse<List<String>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<String> pageBillIoData = billIoService.getBillStatus(billType);
            result.setData(pageBillIoData);
        } catch (BaseException e) {
            LOGGER.error("/BillIoController.getBillStatus ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.getBillStatus ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/batchPushToBms")
    @ApiOperation(value = "默认对账成功, 批量推送BMS", notes = "默认对账成功, 批量推送BMS", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> batchPushToBms(@RequestBody CommonVO<String> billIoCodeList) {
        RestfulResponse<Object> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            if (billIoCodeList != null && billIoCodeList.getEntityParams() != null) {
                billIoService.batchPushToBms(billIoCodeList.getEntityParams());
            }
        } catch (BaseException e) {
            LOGGER.error("BillIoController.batchPushToBms ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("BillIoController.batchPushToBms ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 批量删除
     *
     * @param billIoIdList
     * @return
     */
    @PostMapping(value = "/batchDeleteBillIo")
    @ApiOperation(value = "默认对账成功, 批量推送BMS", notes = "默认对账成功, 批量推送BMS", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> batchDeleteBillIo(@RequestBody CommonVO<Long> billIoIdList) {
        RestfulResponse<Object> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            // if (billIoCodeList != null && billIoCodeList.getEntityParams() != null) {
            //     billIoService.batchPushToBms(billIoCodeList.getEntityParams());
            // }
            if (CollectionUtils.isEmpty(billIoIdList.getEntityParams())) {
                result.setCode(CommonEnum.ERROR.getCode());
                result.setMessage("请选择数据");
                return result;
            }
            billIoService.batchDeleteBillIo(billIoIdList.getEntityParams());
        } catch (BaseException e) {
            LOGGER.error("BillIoController.batchDeleteBillIo ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("BillIoController.batchDeleteBillIo ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
}

